#!/bin/bash

set -euvo pipefail

ENV=$1
TAR_NAME=$2
WORK_DIR=$3
REMOTE_DIR=$4
MODULE_NAME=$5
REMOTE_TARS_DIR=$REMOTE_DIR/tars
OPS_USER="tptbops"

shift 5

for host in "$@"
do
  echo "上传 tar 包和 scripts 到服务器: $host"
  ssh $OPS_USER@$host mkdir -p $REMOTE_TARS_DIR
  scp $WORK_DIR/$TAR_NAME.tar.gz $OPS_USER@$host:$REMOTE_TARS_DIR
  scp -r $WORK_DIR/scripts $OPS_USER@$host:$REMOTE_TARS_DIR
  echo "部署应用到服务器: $host"
  ssh $OPS_USER@$host $REMOTE_TARS_DIR/scripts/deploy.sh $ENV $TAR_NAME $REMOTE_DIR $host $MODULE_NAME
  echo "**********************"
  echo "   $host 部署完成   "
  echo "**********************"
done
